// pages/index/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showMenu: false,
    showMenuPic: false,
    lottery_list: [],
    carousel_list: [],
    try_list: []
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getCarousel();
    that.getLottery();
    that.getTry();
    showMenu: (options.showMenu == "true" ? true : false);
    showMenuPic: (options.showMenuPic == "true" ? true : false);
  },

  onChangeShowMenu: function () {
    var that = this;
    that.setData({
      showMenu: (!that.data.showMenu),
      showMenuPic: (!that.data.showMenuPic)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.globalData.is_login = 0;
    var that = this;
    that.getLottery();
    that.getCarousel();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取抽奖
   */
  getLottery: function () {
    var that = this;
    tt.request({
      url: app.globalData.url + 'index.php?act=toutiao_index&op=index',
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        tt.hideLoading();
        var lottery_list = []
        if (res.data.datas.list.length > 2) {
          for (var i = 0; i < 2; i++) {
            lottery_list.push(res.data.datas.list[i]);
          }
        } else {
          for (var i = 0; i < res.data.datas.list.length; i++) {
            lottery_list.push(res.data.datas.list[i]);
          }
        }

        console.log(lottery_list);
        that.setData({
          lottery_list: lottery_list
        });

      },
      fail(res) {
        tt.hideLoading();
        console.log(`request调用失败`);
      }
    })
  },

  //获取轮播
  getCarousel: function () {
    var that = this;
    tt.request({
      url: app.globalData.url + 'index.php?act=toutiao_index&op=toutiao_carousel',
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        if (res.data.code == 200) {
          var tt_carousel = [];
          if (res.data.datas.tt_carousel != undefined) {
            tt_carousel = res.data.datas.tt_carousel
          }

          that.setData({
            carousel_list: tt_carousel
          })
        } else {
          tt.showToast({
            title: res.data.datas.error
          });
        }
      },
      fail(res) {
        console.log(`request调用失败`);
      }
    })
  },

  getTry: function () {
    var that = this;
    tt.request({
      url: app.globalData.url + 'index.php?act=toutiao_index&op=get_try_list',
      data: {
        pagenum: '4'
      },
      success(res) {
        if (res.data.code == 200) {
          that.setData({
            try_list: res.data.datas.list ? res.data.datas.list : []
          })
          console.log(that.data.try_list);
        } else {
          tt.showToast({
            title: res.data.datas.error
          });
        }
      },
      fail(res) {
        console.log(`request调用失败`);
      }
    })
  }
  // onPullDownRefresh() {
  //   tt.showToast({
  //     title: '刷新...'
  //   });
  //   tt.stopPullDownRefreshSync();
  //   this.getCarousel();
  //   this.getLottery();
  // }
})